package com.hbx.educenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hbx.commonutils.JwtUtils;
import com.hbx.commonutils.MD5;
import com.hbx.commonutils.R;
import com.hbx.educenter.entity.UcenterMember;
import com.hbx.educenter.entity.vo.RegisterVo;
import com.hbx.educenter.mapper.UcenterMemberMapper;
import com.hbx.educenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbx.servicebase.exceptionhandler.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author hbx
 * @since 2023-10-15
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    /**
     * 登录方法
     * @param ucenterMember
     * @return
     */
    @Override
    public String login(UcenterMember ucenterMember) {
        String mobile = ucenterMember.getMobile();
        String password = ucenterMember.getPassword();

        if(StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)){
            throw new GuliException(20001,"登陆失败，用户不存在");
        }

        LambdaQueryWrapper<UcenterMember> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UcenterMember::getMobile,mobile);
        UcenterMember member = this.getOne(queryWrapper);

        if(member == null){
            throw new GuliException(20001,"登陆失败，用户不存在");
        }

        //将password进行加密后再比较
        if(!MD5.encrypt(password).equals(member.getPassword()))
            throw new GuliException(20001,"登陆失败。密码不正确");

        if(member.getIsDeleted())
            throw new GuliException(20001,"登陆失败，已删除");

        //登陆成功之后，使用jwt工具类，生成token值
        String jwtToken = JwtUtils.getJwtToken(member.getId(), member.getNickname());
        return jwtToken;
    }

    /**
     * 注册方法
     * @param register
     */
    @Override
    public void register(RegisterVo register) {
        String mobile = register.getMobile();
        String nickname = register.getNickname();
        String password = register.getPassword();
        String code = register.getCode();

        //判断是否为空
        if(StringUtils.isEmpty(mobile) || StringUtils.isEmpty(nickname)
                || StringUtils.isEmpty(password) || StringUtils.isEmpty(code)){
            throw new GuliException(20001,"注册失败");
        }

        //向redis取出验证码，判断是否一样
        String redisCode = redisTemplate.opsForValue().get(mobile);
        if(!redisCode.equals(code))
            throw new GuliException(20001,"验证码不正确");

        LambdaQueryWrapper<UcenterMember> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UcenterMember::getMobile,mobile);
        UcenterMember member = this.getOne(queryWrapper);
        if(member != null){
            throw new GuliException(20001,"手机号已注册");
        }

        UcenterMember ucenterMember = new UcenterMember();
        ucenterMember.setMobile(mobile);
        ucenterMember.setPassword(MD5.encrypt(password));
        ucenterMember.setNickname(nickname);
        ucenterMember.setIsDeleted(false);
        ucenterMember.setAvatar("https://hbx-2023.oss-cn-beijing.aliyuncs.com/guli/692795a522634619b6666f23c71bed74file.png");
        this.save(ucenterMember);
    }

    @Override
    public UcenterMember getOpenIdMember(String openId) {
        LambdaQueryWrapper<UcenterMember> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UcenterMember::getOpenid,openId);
        UcenterMember member = this.getOne(queryWrapper);
        return member;
    }

    @Override
    public Integer countRegisterDay(String day) {
        return baseMapper.countRegisterDay(day);
    }
}
